<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\exception\DbException;
use think\response\Json;

/**
 * 移动年度收入
 *
 * @icon fa fa-circle-o
 */
class Ydcaiwushow extends Backend
{

    /**
     * Ydcaiwushow模型对象
     * @var \app\admin\model\Ydcaiwushow
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Ydcaiwushow;
        $this->view->assign("ctypeList", $this->model->getCtypeList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        $input = input('param.');
        $filter = isset($input['filter'])?json_decode($input['filter'],true):'';
        if(empty($filter['year'])){
            $year = date('Y')-1;
            $year = 2024;
        }else{
            $year = $filter['year'];
        }

        $list = $this->model
            //->where($where)
            //->order($sort, $order)
            ->order('id asc')
            ->paginate(30);
        foreach($list as $row){
            if($row['shouru'] == '工号(佣金)'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year);
                }
            }
            if($row['shouru'] == '佣金(移动)'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'yd_yj');
                }
            }
            if($row['shouru'] == '佣金(移动家宽)'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'ydjk_yj');
                }
            }
            if($row['shouru'] == '佣金(铁通)'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'tt_yj');
                }
            }
            if($row['shouru'] == '佣金(亿诚)'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'yc_yj');
                }
            }
            if($row['shouru'] == '佣金合计：'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $sum = (float)$list[0][$yue]+(float)$list[1][$yue]+(float)$list[2][$yue]+(float)$list[3][$yue]+(float)$list[4][$yue];
                    $row->$yue = $sum?$sum:'';
                }
            }
            if($row['shouru'] == '移机'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'yiji');
                }
            }
            if($row['shouru'] == '初装费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'chuzhuang');
                }
            }
            if($row['shouru'] == '路由器'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'luyou');
                }
            }
            if($row['shouru'] == '光猫'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'xufei');
                }
            }
            if($row['shouru'] == '其他' &&$row['ctype'] == 1){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'ru_other');
                }
            }
            if($row['shouru'] == '合计收入：'){
                $rang = range(1,12);
                foreach ($rang as $v) {
                    $yue = 'yue' . $v;
                    $arr = [$list[6][$yue], $list[7][$yue], $list[8][$yue], $list[9][$yue], $list[10][$yue]];
                    $sum = array_sum($arr);
                    $row->$yue = $sum==0?'':float_two($sum)+0;
                }
            }
            if($row['shouru'] == '路由器、光猫'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'ly_gm');
                }
            }
            if($row['shouru'] == '其他' &&$row['ctype'] == 2){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'zhi_other');
                }
            }
            if($row['shouru'] == '做任务支出：'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'zhi_rw');
                }
            }
            if($row['shouru'] == '记账费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'jzf');
                }
            }
            if($row['shouru'] == '工资'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'gz');
                }
            }
            if($row['shouru'] == '凡尔赛税费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'fes');
                }
            }
            if($row['shouru'] == '社保费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'sbf');
                }
            }
            if($row['shouru'] == '对公户杂费'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'dghzf');
                }
            }
            if($row['shouru'] == '合计支出：'){
                $rang = range(1,12);
                foreach ($rang as $v) {
                    $yue = 'yue' . $v;
                    $arr = [
                        $list[13][$yue], $list[14][$yue], $list[15][$yue], $list[16][$yue],$list[17][$yue],
                        $list[18][$yue],$list[19][$yue],$list[20][$yue],
                    ];
                    $sum = array_sum($arr);
                    $row->$yue = $sum==0?'':float_two($sum)+0;
                }
            }
            if($row['shouru'] == '对公户其他收入：'){
                $rang = range(1,12);
                foreach ($rang as $v){
                    $yue = 'yue'.$v;
                    $row->$yue = ydyj_find($v,$year,'dg_shouru');
                }
            }
            if($row['shouru'] == '利润：'){
                $rang = range(1,12);
                foreach ($rang as $v) {
                    $yue = 'yue' . $v;
                    $n = (float)$list[5][$yue]+(float)$list[11][$yue]-(float)$list[21][$yue]+(float)$list[22][$yue];
                    $row->$yue = $n?float_two($n):'';
                }
            }
            $row->year = $year;
            $heji = array_sum([
                $row->yue1,$row->yue2,$row->yue3,$row->yue4,$row->yue5,$row->yue6,
                $row->yue7,$row->yue8,$row->yue9,$row->yue10,$row->yue11,$row->yue12]);
            $row->heji = float_two($heji);

        }

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }



}
